package com.itfreer.gis.datasyn.sub;

import android.util.Log;

import com.itfreer.data.remote.DataService;
import com.itfreer.gis.GisDatabaseHelper;
import com.itfreer.gis.datasyn.entity.SynLogEntity;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;

import java.sql.SQLException;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.Callable;

/**
 * 定义同步日志操作服务类
 */
public class SynLogService {

    private static final String dataSynSN = "gisdatasyn";

    public static final SynLogService instance = new SynLogService();

    private SynLogService() {
    }

    /**
     * 添加同步日志
     * @return
     */
    public SynLogEntity addUpLog(){
        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(GisDatabaseHelper.getInstance().getConnectionSource());

        // 开始一个同步
        final SynLogEntity log = new SynLogEntity();
        final String id = UUID.randomUUID().toString();
        log.setId(id);
        log.setSource("移动端同步");
        log.setSynDate(new Date());
        log.setSynUserID("");
        log.setSynUserName("");
        log.setState("50");
        log.setSynType("00");
        log.setReport("启动数据上传同步！\n");

        //一个调用的事件
        Callable<SynLogEntity> callable  = new Callable<SynLogEntity>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public SynLogEntity call() throws Exception {
                // 先在本地创建一个日志
                Dao<SynLogEntity, String> dao = GisDatabaseHelper.getInstance().getDao(SynLogEntity.class);
                dao.create(log);

                // 同步到服务端
                SynLogEntity upLog = DataService.instance.add(dataSynSN, log, SynLogEntity.class);

                // 并且更新本地状态
                if(upLog!=null){
                    dao.update(upLog);
                }else{
                    throw new RuntimeException("远程同步失败！");
                }
                return upLog;
            }
        };

        try {
            //执行事件
            return transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 添加同步日志
     * @return
     */
    public SynLogEntity addDownLog(){
        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(GisDatabaseHelper.getInstance().getConnectionSource());

        // 开始一个同步
        final SynLogEntity log = new SynLogEntity();
        final String id = UUID.randomUUID().toString();
        log.setId(id);
        log.setSource("移动端同步");
        log.setSynDate(new Date());
        log.setSynUserID("");
        log.setSynUserName("");
        log.setState("00");
        log.setSynType("01");
        log.setReport("启动数据下载同步！\n");

        //一个调用的事件
        Callable<SynLogEntity> callable  = new Callable<SynLogEntity>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public SynLogEntity call() throws Exception {
                // 先在本地创建一个日志
                Dao<SynLogEntity, String> dao = GisDatabaseHelper.getInstance().getDao(SynLogEntity.class);
                dao.create(log);

                // 同步到服务端
                SynLogEntity upLog = DataService.instance.add(dataSynSN, log, SynLogEntity.class);

                // 并且更新本地状态
                if(upLog!=null){
                    dao.update(upLog);
                }else{
                    throw new RuntimeException("远程同步失败！");
                }
                return upLog;
            }
        };

        try {
            //执行事件
            return transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 更新同步日志
     * @return
     */
    public SynLogEntity updateLog(final SynLogEntity log){
        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(GisDatabaseHelper.getInstance().getConnectionSource());
        //一个调用的事件
        Callable<SynLogEntity> callable  = new Callable<SynLogEntity>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public SynLogEntity call() throws Exception {
                // 先更新到同地
                Dao<SynLogEntity, String> dao = GisDatabaseHelper.getInstance().getDao(SynLogEntity.class);
                dao.update(log);

                // 同步到服务端
                SynLogEntity upLog = DataService.instance.update(dataSynSN, log, SynLogEntity.class);
                // 并且更新本地状态
                if(upLog!=null){
                    dao.update(upLog);
                }else{
                    throw new RuntimeException("远程同步失败！");
                }
                return upLog;
            }
        };

        try {
            //执行事件
            return transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 与服务器端同步日志
     * @param synid
     * @return
     */
    public SynLogEntity synLog(final String synid) {
        TransactionManager transactionManager = new TransactionManager(GisDatabaseHelper.getInstance().getConnectionSource());
        //一个调用的事件
        Callable<SynLogEntity> callable  = new Callable<SynLogEntity>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public SynLogEntity call() throws Exception {
                Dao<SynLogEntity, String> dao = GisDatabaseHelper.getInstance().getDao(SynLogEntity.class);
                SynLogEntity log = DataService.instance.getEntity(dataSynSN, synid, SynLogEntity.class);
                if(log!=null){
                    dao.update(log);
                    return log;
                }else{
                    throw new RuntimeException("远程同步失败！");
                }
            }
        };

        try {
            //执行事件
            return transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
            return null;
        }
    }
}
